package com.zj.leetcode.easy;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @program: algorithm
 * @description: 加一
 * 66 plus-one
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class PlusOne {
    public static void main(String[] args) {
//        int[] digits = new int[]{1, 2, 3};
        int[] digits = new int[]{9, 9, 9};
        Solution solution = new PlusOne().new Solution();

        System.out.println(Arrays.toString(solution.plusOne(digits)));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int[] plusOne(int[] digits) {
            List<Integer> list = new ArrayList<>();

            int num = 0;
            int carryBit = 0;
            for (int i = digits.length - 1; i >= 0; i--) {

                if (i == digits.length - 1) {
                    num = digits[i] + carryBit + 1;
                    carryBit = num / 10;
                } else {
                    if (carryBit > 0) {
                        num = digits[i] + carryBit;
                        carryBit = num / 10;
                    } else {
                        num = digits[i];
                    }
                }
                if (num >= 10) {
                    list.add(num % 10);
                } else {
                    list.add(num);
                }
            }
            if (carryBit != 0) {
                list.add(carryBit);
            }

            int[] ret = new int[list.size()];
            for (int i = list.size() - 1; i >= 0; i--) {
                ret[list.size() - i - 1] = list.get(i);
            }
            return ret;

        }

//        public int[] plusOne02(int[] digits) {
//
//        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
